<?php
	defined('RUN') or die('No access');
	
	class AdvModel
	{
		private $tables='adv';
		public function getCategories()
		{
			return Model::getDataSql("SELECT * FROM `adv_category` ORDER BY `id` ASC");
		}
	    public function getAdvs($fields='*',$parent=null)
		{
			if($parent != null)
				return Model::getDataSql("SELECT ".$fields." FROM ".$this->tables." WHERE `category`=".$parent." ORDER BY `sort` ASC, `id` DESC");
			else
				return Model::getDataSql("SELECT ".$fields." FROM ".$this->tables." ORDER BY `sort` ASC, `id` DESC");
		}
		 public function getAdvadm($fields='*')
		{
			return Model::getDataSql("SELECT ".$fields.",`adv_category`.`name` as catename FROM ".$this->tables." LEFT JOIN `adv_category` ON `adv`.`category` = `adv_category`.`id` ORDER BY `sort` ASC, `id` DESC");
		}
		public function getById($id,$fields='*')
		{
			$condition = array(
					'field' => 'id',
					'value' => $id
				);

			$cat = Model::getData($this->tables, $fields, $condition);
			if ($cat)
				return $cat[0];

			return FALSE;
		}
		public function add($data)
		{
			return Model::insertData($this->tables, $data);
		}

		public function update($data, $id)
		{
			$condition = array(
					'field' => 'id',
					'value' => $id
				);
			
			return Model::updateData($this->tables, $data, $condition);
		}

		public function delete($id)
		{
			$condition = array(
					'field' => 'id',
					'value' => $id
				);

			return Model::deleteData($this->tables, $condition);
		}
	}
